package com.property.manage.service.dict;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guardchina.framework.mybatis.model.CustomPage;
import com.guardchina.framework.util.exception.BusinessException;
import com.guardchina.framework.util.utils.DateUtil;
import com.guardchina.framework.util.utils.PhoneUtil;
import com.property.manage.dao.dict.DictMapper;
import com.property.manage.dao.equipment.MaintenanceMapper;
import com.property.manage.model.dict.DictEntity;
import com.property.manage.model.equipment.MaintenanceEntity;
import com.property.manage.model.equipment.MaintenanceVo;
import com.property.manage.service.equipment.EquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;

/**
 * 描述
 *
 * @author lwb
 * @since 2020/7/15
 */
@Component
@Slf4j
public class DictService {
    @Autowired
    private DictMapper dictMapper;



    public DictEntity findDict(String code) {
        QueryWrapper<DictEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("code",code);
        return dictMapper.selectOne(queryWrapper);
    }


    public void add(String name, String code,Integer value,Integer superiorId
    ) {
        if (findDict(code)!=null){
            throw new BusinessException("新增字典code重复错误");
        }
        try{
            DictEntity entity = new DictEntity();
            entity.setCode(code);
            entity.setName(name);
            entity.setValue(value);
            entity.setSuperiorId(superiorId);
            dictMapper.insert(entity);
        }catch (Exception e){
            throw new BusinessException("新增记录失败",String.format("新增字典失败[%s]",e));
        }
    }

    public void delete(Integer id) {dictMapper.deleteById(id);
    }


    public List<DictEntity> findBySuperior(Integer superiorId){
        QueryWrapper<DictEntity> qw=new QueryWrapper<>();
        qw.eq("superior_id",superiorId);
       return dictMapper.selectList(qw);
    }

    public List<DictEntity> findByCode(String code){
        DictEntity dict = findDict(code);
        if (dict==null){
            return Collections.emptyList();
        }
        return findBySuperior(dict.getId());
    }

}
